﻿using System;
using System.Collections;
using System.Configuration;
using System.IO;
using System.Data;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using PlugNT.Custom;
using PlugNT.Cms.BLL;
using PlugNT.Cms.Core;
using PlugNT.Cms.Page;

namespace PlugNT.Cms.Web.Admin.Pre_Systems
{
    public partial class Pre_Manager : AdminPage
    {
        private Templet curTemplet = new Templet();
        private const string sitemapfile = "sitemap.xml";

        protected void Page_Load(object sender, EventArgs e)
        {
            //权限检测
            this.CheckPower();

            string action = (Request.QueryString["a"] == null) ? "" : Request.QueryString["a"].Trim();
            if (action != "")
            {
                if (action == "clearcache")
                {
                    btnCache_Click(sender, new EventArgs());
                }
                else if (action == "resetapp")
                {
                    btnReLoad_Click(sender, new EventArgs());
                }
            }
        }

        #region 加载数据
        private void ShowCaches()
        {
            //显示所有缓存
            StringBuilder sbCache = new StringBuilder();
            IDictionaryEnumerator CacheEnum = HttpRuntime.Cache.GetEnumerator();
            while (CacheEnum.MoveNext())
            {
                sbCache.Append("缓存名<b>[" + CacheEnum.Key + "]</b><br />");
            }
            this.litCacheMessage.Text = "当前网站总缓存数:" + HttpRuntime.Cache.Count + "<br />" + sbCache.ToString();
        }
        #endregion

        #region 清空管理
        //清除所有缓存
        protected void btnCache_Click(object sender, EventArgs e)
        {
            System.Web.Caching.Cache _cache = HttpRuntime.Cache;
            IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
            ArrayList al = new ArrayList();
            while (CacheEnum.MoveNext())
            {
                al.Add(CacheEnum.Key);
            }
            foreach (string key in al)
            {
                _cache.Remove(key);
            }

            //显示缓存
            ShowCaches();
        }

        //应用程序重启
        protected void btnReLoad_Click(object sender, EventArgs e)
        {
            litCacheMessage.Text = "应用程序已经重启了";
            HttpRuntime.UnloadAppDomain();
        }

        //内存释放
        protected void btnMemory_Click(object sender, EventArgs e)
        {
            string error = PlugNT.Common.RevokeMemory.ReduceMemoryFootPrint();
            if (error != "")
            {
                PlugNT.Custom.SysManager.LogErrorWriteln(error);
                litCacheMessage.Text = error;
            }
            else
            {
                litCacheMessage.Text = "当前内存已释放";
            }
        }  
        #endregion

        #region 清除数据表
        protected void btnClearTable_Click(object sender, EventArgs e)
        {
            string tabName = txtTableName.Text.Trim();
            string idName = txtIdName.Text.Trim();
            if (tabName == "" || idName == "")
            {
                this.litTablesMessage.Text = "<font style=\"color:red;\">" + "请输入表名或id名" + "</font>";
                return;
            }
            try
            {
                PlugNT.Cms.Core.DataAccess.ReSetTable(tabName, idName);
                this.litTablesMessage.Text = tabName + "表已经清空了";
            }
            catch (Exception ex)
            {
                this.litTablesMessage.Text = "<font style=\"color:red;\">" + ex.Message + "</font>";
            }
        }
        #endregion







    }
}
